const { defineConfig } = require('@vue/cli-service')
const { list } = require('./src/mock')
const bodyParser = require('body-parser')
module.exports = defineConfig({
  transpileDependencies: true,
  devServer: {
    setupMiddlewares(middle, { app }) {
      app.use(bodyParser.json())
      app.get('/api/list', (req, res) => {
        res.send({
          code: 200,
          data: list
        })
      })

      app.post('/api/list/state/top', (req, res) => {
        const { id } = req.body;
        const item = list.find(v => v.id === id)
        item.isTop = !item.isTop;
        res.send({
          code: 200,
          message: '状态以改变'
        })
      })

      app.post('/api/list/state/common', (req, res) => {
        const { id } = req.body;
        const item = list.find(v => v.id === id)
        item.isCommon = !item.isCommon;
        res.send({
          code: 200,
          message: '状态以改变'
        })
      })

      app.post('/api/list/state/use', (req, res) => {
        const { id } = req.body;
        const item = list.find(v => v.id === id)
        item.isUse = !item.isUse;
        res.send({
          code: 200,
          message: '状态以改变'
        })
      })

      app.get('/api/2002a/list', (req, res) => {
        const obj = {
          topData: [],
          list: []  
        }
        list.forEach(v => {
          if(!v.isUse) return 
          if(v.isTop) {
            obj.topData.push({ ...v })
          } else {
            obj.list.push({ ...v })
          }
        })

        res.send({
          code: 200,
          data: obj
        })
      })


      app.post('/api/lsit/add', (req, res) => {
        req.body.id = Math.random().toString().slice(2)
        list.unshift(req.body);
        res.send({
          code: 200
        })
      })

      return middle
    }
  }
})
